#include "hal_system_status.h"
#include "hk32c030xx_gpio.h"

static void system_fault_init() {
	
	GPIO_InitTypeDef sys_key;
	RCC_AHBPeriphClockCmd(SYSTEM_STATUS_RCU | SYSTEM_RELAY_RCU, ENABLE);
	
	sys_key.GPIO_Pin = SYSTEM_BAND_BREAK_PIN | SYSTEM_MOTOR_RUN_PIN;
	sys_key.GPIO_Mode = GPIO_Mode_IN;
	sys_key.GPIO_OType = GPIO_OType_PP;
	sys_key.GPIO_Speed = GPIO_Speed_10MHz;
	sys_key.GPIO_PuPd = GPIO_PuPd_NOPULL;
	sys_key.GPIO_Schmit = GPIO_Schmit_Disable;
	GPIO_Init(SYSTEM_STATUS_BASE, &sys_key);

	sys_key.GPIO_Pin = SYSTEM_RELAY_PIN;
	GPIO_Init(SYSTEM_RELAY_BASE, &sys_key);
}

static uint8_t get_system_state() {
	
	if(GPIO_ReadInputDataBit(SYSTEM_STATUS_BASE,SYSTEM_BAND_BREAK_PIN) == 0)
		return SYS_FAULT_BAND_BREAK;
		
	
	if(GPIO_ReadInputDataBit(SYSTEM_RELAY_BASE,SYSTEM_RELAY_PIN) == 0)
		return SYS_FAULT_RELAY;
	
	return SYS_FAULT_NORMAL;
}

static uint8_t get_system_running_state() {
	
	if(GPIO_ReadInputDataBit(SYSTEM_STATUS_BASE,SYSTEM_MOTOR_RUN_PIN))
		return 0;
	else 
		return 1;
}

const system_fault_t g_sys_ = {
	system_fault_init,
	get_system_state,
	get_system_running_state
};
